package xcmg.device.web.warehouse;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.InventoryCheckReportDO;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanyInfoService;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.vo.basic.MaterialsRelationVO;
import xcmg.device.service.vo.warehouse.InvenitoryWarehouseSearchVO;
import xcmg.device.service.vo.warehouse.InventoryCheckDetailVO;
import xcmg.device.service.vo.warehouse.InventoryCheckVO;
import xcmg.device.service.vo.warehouse.InventoryMaterialsSearchVO;
import xcmg.device.service.warehouse.InventoryCheckService;
import xcmg.device.service.warehouse.WarehouseService;
import xcmg.device.util.CompanyUtil;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.ListUtil;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/inventoryCheck")
public class InventoryCheckController extends BasicController {
    @Autowired
    private InventoryCheckService inventoryCheckService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private CompanyInfoService companyInfoService;

    @RequestMapping(value = "/runCheck", method = RequestMethod.POST)
    @ApiOperation(value = "开始盘点")
    @FastMappingInfo(needLogin = true, code = AuthCode.InventoryCheck.OPEN)
    public ActionResult runCheck(@RequestBody InventoryCheckDetailVO model, @RequestHeader("x-user-id") String userId) throws Exception {
        return actionResult(inventoryCheckService.runCheck(model.getId(), userId));
    }

    @RequestMapping(value = "/stopCheck", method = RequestMethod.POST)
    @ApiOperation(value = "结束盘点")
    @FastMappingInfo(needLogin = true, code = AuthCode.InventoryCheck.CLOSE)
    public ActionResult stopCheck(@RequestBody InventoryCheckDetailVO model) throws Exception {
        return actionResult(inventoryCheckService.stopCheck(model.getId()));
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "查询盘点单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(@RequestParam("inventoryCheckId") String inventoryCheckId) throws Exception {
        return actionValueResult(inventoryCheckService.item(inventoryCheckId));
    }

    @RequestMapping(value = "/printItem", method = RequestMethod.GET)
    @ApiOperation(value = "查询盘点单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult printItem(@RequestParam("inventoryCheckId") String inventoryCheckId) throws Exception {
        return actionValueResult(inventoryCheckService.printItem(inventoryCheckId));
    }

    @RequestMapping(value = "/itemCount", method = RequestMethod.GET)
    @ApiOperation(value = "查询盘点情况")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemCount(@RequestParam("checkOrderNo") String checkOrderNo) throws Exception {
        InventoryCheckDetailVO detailVO = new InventoryCheckDetailVO();

        detailVO.setOrgId(getOrgId());
        detailVO.setCheckOrderNo(checkOrderNo);
        return actionValueResult(inventoryCheckService.itemCount(detailVO));
    }


    @RequestMapping(value = "/itemList", method = RequestMethod.POST)
    @ApiOperation(value = "查询盘点单详情列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemList(@RequestBody SearchCommonVO<InventoryCheckDetailVO> condition) throws Exception {
        InventoryCheckDetailVO detailVO = condition.getFilters();
        if (detailVO == null) {
            detailVO = new InventoryCheckDetailVO();
        }
        detailVO.setOrgId(getOrgId());
        condition.setFilters(detailVO);
        PageCommonVO pageCommonVO = inventoryCheckService.itemList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/itemByBarCode", method = RequestMethod.GET)
    @ApiOperation(value = "扫描条码查询信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult itemByBarCode(@RequestParam("barCode") String barCode, @RequestParam("checkOrderNo") String checkOrderNo) throws Exception {
        return actionValueResult(inventoryCheckService.itemByBarCode(barCode, checkOrderNo));
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("查询盘点单记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<InventoryCheckVO> condition) throws Exception {
        InventoryCheckVO inventoryCheckVO = condition.getFilters();
        if (inventoryCheckVO == null) {
            inventoryCheckVO = new InventoryCheckVO();
        }
        String orgId = getOrgId();
        String deptId = getDeptId();
        inventoryCheckVO.setOrgId(orgId);
        condition.setFilters(inventoryCheckVO);
        String sapCode = companyInfoService.getSapCodeByOrgId(orgId);
        PageCommonVO pageCommonVO;
        if (CompanyUtil.isTJSapCode(sapCode)) {
            pageCommonVO = inventoryCheckService.warehosueInventoryList(condition, deptId);
        } else {
            pageCommonVO = inventoryCheckService.list(condition);
        }

        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加盘点单记录")
    @FastMappingInfo(needLogin = true, code = AuthCode.InventoryCheck.ADD)
    public ActionResult add(@RequestBody InventoryCheckVO inventoryCheckVO, @RequestHeader("x-user-id") String userId) throws Exception {
        if (null == inventoryCheckVO) {
            return actionResult(ErrorCode.IllegalArument);
        }

        inventoryCheckVO.setOrgId(getOrgId());
        inventoryCheckVO.setCreateId(userId);
        inventoryCheckVO.setCreateName(getUserName());
        Integer checkType = inventoryCheckVO.getCheckType();
        if (checkType != null && checkType.compareTo(4) == 0) {//按照仓库类型盘点单独逻辑
            inventoryCheckService.warehouseTypeCheckAdd(inventoryCheckVO);
            return actionResult(ErrorCode.Success);
        } else {
            return actionResult(inventoryCheckService.add(inventoryCheckVO));
        }
    }

    @RequestMapping(value = "/inventoryCheck", method = RequestMethod.POST)
    @ApiOperation("盘点确认")
    @FastMappingInfo(needLogin = true)
    public ActionResult inventoryCheck(@RequestBody InventoryCheckDetailVO checkDetailVO) throws Exception {
        checkDetailVO.setOrgId(getOrgId());
        checkDetailVO.setInventoryUserId(getUserId());
        return actionResult(inventoryCheckService.inventoryCheck(checkDetailVO));
    }

    @RequestMapping(value = "/enforce", method = RequestMethod.POST)
    @ApiOperation("强制完成盘点")
    @FastMappingInfo(needLogin = true)
    public ActionResult enforce(@RequestBody InventoryCheckVO inventoryCheckVO, @RequestHeader("x-user-id") String userId) throws Exception {
        inventoryCheckVO.setOrgId(getOrgId());
        return actionResult(inventoryCheckService.enforce(inventoryCheckVO, userId));
    }

    @RequestMapping(value = "/selectUserByWarehouse", method = RequestMethod.GET)
    @ApiOperation(value = "查询仓库对应保管员")
    @FastMappingInfo(needLogin = true)
    public ActionResult selectUserByWarehouse(String warehouseCode) throws Exception {
        List<MaterialsRelationVO> results = inventoryCheckService.selectUserByWarehouse(warehouseCode, getOrgId());
        return actionValueResult(results);
    }

    @RequestMapping(value = "/invenitoryMaterialsList", method = RequestMethod.POST)
    @ApiOperation(value = "预览")
    @FastMappingInfo(needLogin = true)
    public ActionResult invenitoryMaterialsList(@RequestBody SearchCommonVO<InventoryMaterialsSearchVO> searchVO) throws Exception {
        InventoryMaterialsSearchVO inventoryMaterialsSearchVO = searchVO.getFilters();
        if (searchVO.getFilters() == null) {
            inventoryMaterialsSearchVO = new InventoryMaterialsSearchVO();
        }
        if (ListUtil.isNullOrEmpty(inventoryMaterialsSearchVO.getLocationCodes())) {
            inventoryMaterialsSearchVO.setLocationCodes(null);
        }
        inventoryMaterialsSearchVO.setOrgId(getOrgId());
        searchVO.setFilters(inventoryMaterialsSearchVO);
        PageCommonVO pageCommonVO = inventoryCheckService.invenitoryMaterialsList(searchVO);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/invenitoryWarehouseList", method = RequestMethod.POST)
    @ApiOperation(value = "仓库类型预览")
    @FastMappingInfo(needLogin = true)
    public ActionResult invenitoryWarehouseList(@RequestBody SearchCommonVO<InvenitoryWarehouseSearchVO> searchVO) throws Exception {
        InvenitoryWarehouseSearchVO filters = searchVO.getFilters();
        //参数校验
        if (filters == null || StringUtils.isEmpty(filters.getWarehouseType())) {
            return ActionResultUtil.errorActionResult(ErrorCode.Failure);
        }
        String orgId = getOrgId();
        searchVO.getFilters().setOrgId(orgId);
        //根据仓库类型获取仓库列表
        PageCommonVO warehouses = warehouseService.getWarehouseByTypePage(searchVO);
        return ActionResultUtil.successActionResult(warehouses.getPageInfo());
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation(value = "删除盘点单")
    @FastMappingInfo(needLogin = true, code = AuthCode.InventoryCheck.DEL)
    public ActionResult selectUserByWarehouse(@RequestBody String[] ids) throws Exception {
        if (ids == null) {
            return actionResult(ErrorCode.MaterialsNoIsNull);
        }
        ErrorCode ret = inventoryCheckService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/listReport", method = RequestMethod.POST)
    @ApiOperation(value = "盘点单报表")
    @FastMappingInfo(needLogin = true)
    public ActionResult listReport(@RequestBody SearchCommonVO<InventoryCheckVO> condition) throws Exception {
        String orgId = getOrgId();
        condition.getFilters().setOrgId(orgId);
        PageCommonVO pageCommonVO = inventoryCheckService.listReport(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "盘点单报表导出")
    @FastMappingInfo(needLogin = true)
    public void export(@RequestBody List<InventoryCheckReportDO> list) throws Exception {
        inventoryCheckService.export(list);
    }

    @RequestMapping(value = "/exportAll", method = RequestMethod.POST)
    @ApiOperation(value = "盘点单报表导出全部")
    @FastMappingInfo(needLogin = true)
    public void exportAll(InventoryCheckVO condition) throws Exception {
        condition.setOrgId(getOrgId());
        inventoryCheckService.exportAll(condition);
    }

}
